	

	******************************************************************
	**
	**
	**		DETAILS: 	This file takes raw historical survey
	**					data and creates a table of 
	**					proportion of the population in each 
	**					education category group.
	**				
	**
	******************************************************************

		
		
		
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	
*-------------------------------------------------------------------------------
* 1973 Survey
*-------------------------------------------------------------------------------


* raw survey data
use "data/inputs/Survey1973.dta", clear		


* Education groups
gen educ = "No primary" if educ_original == 0 | educ_original == 1
replace educ = "Primary complete" if educ_original == 2 | educ_original == 3
replace educ = "Secondary complete" if educ_original >= 4 & educ_original <= 7
replace educ = "Tertiary complete" if educ_original == 8	

* group indicators
tab educ, gen(educ)	

* collapse
collapse (sum) educ1-educ4

* proportions
egen total = rowtotal(educ1-educ4)
gen MenosDePrimaria = educ1 / total
gen Primaria = educ2 / total
gen Bachiller = educ3 / total
gen Licenciado = educ4 / total

* save
keep Menos-Lic
gen year = 1973
tempfile opinion1973
save `opinion1973'
	
	
	
	
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	
*-------------------------------------------------------------------------------
* 1983 survey
*-------------------------------------------------------------------------------


* raw survey data
use "data/inputs/Survey1983.dta", clear		


* Education groups
gen educ = "No primary" if educ_original == 1	 | educ_original == 2
replace educ = "Primary complete" if educ_original == 3 | educ_original == 4
replace educ = "Secondary complete" if educ_original == 5 | educ_original == 6
replace educ = "Tertiary complete" if educ_original == 7
drop if educ == ""

* group indicators
tab educ, gen(educ)	 

* collapse
collapse (sum) educ1-educ4

* proportions
egen total = rowtotal(educ1-educ4)
gen MenosDePrimaria = educ1 / total
gen Primaria = educ2 / total
gen Bachiller = educ3 / total
gen Licenciado = educ4 / total

* save
keep Menos-Lic
gen year = 1983
tempfile opinion1983
save `opinion1983'
	

		
		
		
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	
*-------------------------------------------------------------------------------
* 1988
*-------------------------------------------------------------------------------
	
	
* raw survey data	
use "data/inputs/Survey1988.dta", clear


* Education groups
gen educ = "No primary" if educ_original == 1	 | educ_original == 2
replace educ = "Primary complete" if educ_original == 3 | educ_original == 4
replace educ = "Secondary complete" if educ_original == 5 | educ_original == 6
replace educ = "Tertiary complete" if educ_original == 7

* group indicators
tab educ, gen(educ)	
	
* collapse
collapse (sum) educ1-educ4

* proportions
egen total = rowtotal(educ1-educ4)
gen MenosDePrimaria = educ1 / total
gen Primaria = educ2 / total
gen Bachiller = educ3 / total
gen Licenciado = educ4 / total

* save
keep Menos-Lic
gen year = 1988
tempfile opinion1988
save `opinion1988'

		
		
		
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	
*-------------------------------------------------------------------------------
* 1993–2006
*-------------------------------------------------------------------------------
	
	
* Lupu replication data	
use "data/inputs/Survey1993_2006_Lupu.dta", clear	


* Education groups
tab educ, gen(educ)

* loop over weighted/unweighted	
local i = 1
foreach weight in "[aw=weight]" " " {

* collapse
preserve
collapse (sum) educ1-educ4 `weight', by(year)

* proportions
egen total = rowtotal(educ1-educ4)
gen MenosDePrimaria`i' = educ1 / total
gen Primaria`i' = educ2 / total
gen Bachiller`i' = educ3 / total
gen Licenciado`i' = educ4 / total

* save
keep year Menos-Lic
gen weight = "`weight'"
tempfile opinionLupu_`i'
save `opinionLupu_`i''

* close loop
local i = `i' + 1
restore
}

* merge weighted + unweighted
clear
use `opinionLupu_1'
merge 1:1 year using `opinionLupu_2'
renvars *1, postfix(_wt)
renvars *2, postdrop(1)
drop weight _m

* save
tempfile opinion1993
save `opinion1993'

		
		
		
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**

*-------------------------------------------------------------------------------
* 2012
*-------------------------------------------------------------------------------


* raw LAPOP data
use "data/inputs/Survey2012_LAPOP.dta", clear


* Education groups
gen educ = "No primary" if educ_original < 6
replace educ = "Primary complete" if educ_original >= 6 & educ_original < 11
replace educ = "Secondary complete" if educ_original >= 11 & educ_original < 15
replace educ = "Tertiary complete" if educ_original >= 15 & educ_original ~= .


* group indicators
tab educ, gen(educ)


* loop over weighted/unweighted
local i = 1
foreach weight in "[fw=wt]" " " {

* collapse
preserve
collapse (sum) educ1-educ4 `weight'

* proportions
egen total = rowtotal(educ1-educ4)
gen MenosDePrimaria`i' = educ1 / total
gen Primaria`i' = educ2 / total
gen Bachiller`i' = educ3 / total
gen Licenciado`i' = educ4 / total

* save
keep Menos-Lic
gen year = 2012
gen weight = "`weight'"
tempfile opinion2012_`i'
save `opinion2012_`i''

* close loop
local i = `i' + 1
restore
}

* merge weighted/unweighted
clear
append using `opinion2012_1'
merge 1:1 year using `opinion2012_2'
renvars *1, postfix(_wt)
renvars *2, postdrop(1)
drop weight _m

* save
tempfile opinion2012
save `opinion2012'

		
		
		
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	
*-------------------------------------------------------------------------------
* append all
*-------------------------------------------------------------------------------

clear 
foreach year in 1973 1983 1988 1993 2012 {
	append using `opinion`year''
}

		
		
		
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**

*-------------------------------------------------------------------------------
* save table 
*-------------------------------------------------------------------------------


* organize
order year
foreach var of varlist Menos* Prim* Bach* Lic* {
	replace `var' = round(`var'*100)
	}
tostring *, replace force


* format
foreach var of varlist Menos* Prim* Bach* Lic* {
	replace `var' = `var' + "\%" if `var' ~= "."
}

	
* save
gen latex_table1 = year
foreach var of varlist MenosDePrimaria-Licenciado1_wt {
	replace latex_table1 = latex_table1 + " & " + `var'
	}
replace latex_table1 = latex_table1 + " \\"
outsheet latex_table1 using "tables/TableB2.tex", noquote nonames replace


* End
